package org.jit.sose.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import org.jit.sose.entity.Tree;
import org.jit.sose.entity.TreeNode;
import org.jit.sose.service.impl.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class TreeController {
    @Autowired
    TreeService treeService;

    @RequestMapping(value = "/getAllTreeNode")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public JSONArray getAllTreeNode() {
        List<TreeNode> treeNodes = treeService.getAllTreeNode();
        JSONArray jsonArray = new Tree().buildTree(treeNodes);
        return jsonArray;
    }

    @RequestMapping(value = "/addTreeNode", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void addTreeNode(@RequestBody String paramTreeNode) {
        TreeNode treeNode = JSON.parseObject(paramTreeNode, new TypeReference<TreeNode>() {
        });
        treeService.addTreeNode(treeNode);
    }

    @RequestMapping(value = "/deleteTreeNode")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void deleteTreeNode(String classifyId) {
        treeService.deleteTreeNode(classifyId);
    }

    @RequestMapping(value = "/updateTreeNode", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void updateTreeNode(@RequestBody String paramTreeNode) {

        TreeNode treeNode = JSON.parseObject(paramTreeNode, new TypeReference<TreeNode>() {
        });
        System.out.println("调用更新接口"+treeNode.getTitle());
        treeService.updateTreeNOde(treeNode);

    }


}
